package com.zl.autocode.dao.generator;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.zl.autocode.common.constant.AutocodeConfigC;
import com.zl.autocode.common.utils.generator.model.ConvertColumn;
import com.zl.autocode.common.utils.generator.model.ConvertTable;

import cn.hutool.extra.spring.SpringUtil;

@Service("hive2"+AutocodeConfigC.GENERATOR_DAO_KEY)
public class Hive2GeneratorDao implements IGeneratorDao{
    private static Logger LOGGER = LoggerFactory.getLogger(Hive2GeneratorDao.class);
    private Connection con =null;
    private Statement st =null;
    @Override
    public List<ConvertTable> queryTables(String tableCodes, String type,boolean endFlag) throws Exception {
        List<ConvertTable> list=new ArrayList<>();
        try{
            String tbls= "";
            if(StringUtils.isNotBlank(tableCodes)){
                tbls= " like " +("1".equals(type)?"'*"+tableCodes.replace("%", "")+"*'":"'"+tableCodes+"'");
            }
            if(con==null || con.isClosed()){
                con = SpringUtil.getBean(DataSource.class).getConnection();
                st = con.createStatement();
            }
            //tab_name
            ResultSet rs = st.executeQuery("show tables"+tbls);
            while (rs.next()){
                ConvertTable vo=new ConvertTable();
                vo.setTableCode(rs.getString(1));
                list.add(vo);
            }
            rs.close();
            
            getTableComment(list);
            
        }catch(Exception e){
            LOGGER.error("数据库查询异常{}",e);
            if(con!=null){
                st.close();
                con.close();
            }
        }finally{
            if(endFlag&&con!=null){
                st.close();
                con.close();
            }
        }
        return list;
    }

    private void getTableComment(List<ConvertTable> list) {
       for(ConvertTable lt:list) {
            try{
                //注释只能单独获取
                st = con.createStatement();
                ResultSet rs = st.executeQuery("DESCRIBE FORMATTED "+lt.getTableCode());
                //col_name  data_type  comment
                while (rs.next()){
                    String str = rs.getString("data_type");
                    if(str!=null && "comment".equals(str.trim())) {
                        String comment = rs.getString("comment");
                        lt.setTableComment(StringUtils.isNotBlank(comment)?comment.trim():null);
                        break;
                    }
                }
                rs.close();
            }catch (Exception e) {
                return;
            }
        }
    }
    
    
    @Override
    public List<ConvertColumn> queryTableColumns(String tableCode,boolean endFlag) throws Exception {
        List<ConvertColumn> list=new ArrayList<>();
        try{
            if(con==null || con.isClosed()){
                con = SpringUtil.getBean(DataSource.class).getConnection();
                st = con.createStatement();
            }
            ResultSet rs = st.executeQuery("desc "+tableCode);
            while (rs.next()){
                String str = rs.getString("col_name");
                if(StringUtils.isBlank(str)) {
                    break;
                }
                //col_name  data_type  comment
                ConvertColumn vo=new ConvertColumn();
                vo.setColumnCode(rs.getString("col_name"));
                vo.setColumnDatatype(rs.getString("data_type"));
                vo.setColumnComment(rs.getString("comment"));
                list.add(vo);
            }
            rs.close();
        }catch(Exception e){
            LOGGER.error("数据库查询异常{}",e);
            if(con!=null){
                st.close();
                con.close();
            }
        }finally{
            if(endFlag&&con!=null){
                st.close();
                con.close();
            }
        }
        return list;
    }
}
